package features.advance.nowcoder.huawei;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author LIN
 * @date 2022-02-16 18:04
 */
public class Interview01 {
    private static Map<Integer,String> map = new HashMap<>();

    public static void main(String[] args) {
//        Scanner in = new Scanner(System.in);
//        while (in.hasNextInt()) {
//            int a = in.nextInt();
//
//            for (int i = 0; i < a; i++) {
//
//            }
//            System.out.println(a);
//        }
        int a = 5;
        if(map.containsKey(a)){
            System.out.println(map.get(a));
        }
        String recursion = "1";
        map.put(0,recursion);
        for (int i = 1; i <= a; i++) {
            recursion = new Interview01().recursion(recursion);
            map.put(i,recursion);
        }
        System.out.println(recursion);
        System.out.println(map);
    }

    public String recursion(String str){
        char[] chars = str.toCharArray();
        Map<Character,Integer> tmp = new LinkedHashMap<>(16);
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < chars.length; i++) {
            char c = chars[i];
            if(tmp.containsKey(c)){
                tmp.put(c,tmp.get(c)+1);
            }else{
                if(!tmp.isEmpty()){
                    for (Map.Entry<Character, Integer> entry : tmp.entrySet()) {
                        Character key = entry.getKey();
                        Integer value = entry.getValue();
                        sb.append(value).append(key);
                    }
                    tmp.clear();
                }
                tmp.put(c,1);
            }

        }
        for (Map.Entry<Character, Integer> entry : tmp.entrySet()) {
            Character key = entry.getKey();
            Integer value = entry.getValue();
            sb.append(value).append(key);
        }

        return sb.toString();
    }
}
